define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'twotg/index' + location.search,
                    add_url: 'twotg/add',
                    edit_url: 'twotg/edit',
                    del_url: 'twotg/del',
                    multi_url: 'twotg/multi',
                    import_url: 'twotg/import',
                    table: 'twotg',
                }
            });

            var table = $("#table");
            let time =setInterval(() => {
                $.ajax({
                    type: 'POST',
                    url: 'twotg/isHaveNewAfter',
                    success: function (data) {
                        if (data.code == 1) {
                            if (data.data > 0) {
                                // 提示音
                                // var audio = document.getElementById('reftips');
                                // audio.play();
                                // 提示框
                                Layer.alert('您有新的报装服务信息，请尽快查看', {
                                    skin: 'layui-layer-molv' //样式类名  自定义样式
                                    , closeBtn: 0    // 是否显示关闭按钮
                                    , anim: 1 //动画类型
                                    , btn: ['马上去处理', '我知道了'] //按钮
                                    , icon: 6    // icon
                                    , yes: function (index) {
                                        handleTui('go');

                                        //audio.pause();
                                        //audio.currentTime = 0;
                                        layer.close(index);
                                    }, btn2: function (index) {
                                        handleTui('know');
                                        //audio.pause();
                                        //audio.currentTime = 0;

                                        layer.close(index);
                                    }
                                });
                            }
                        }
                    }
                });
            }, 1000*30);
            function handleTui(type) {
                $.ajax({
                    type: 'POST',
                    url: 'twotg/handleTui',
                    data: { type: type },
                    async: false,//同步
                    success: function (data) {
                        if (data.code == 1) {
                            if (data.data == 'go') {
                                // 点击菜单 "订单管理"，打开tab页
                                $('.sidebar-menu li a[addtabs=129]').click();
                            }
                        }
                    }
                });
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_id', title: __('订单编号')},
                        {field: 'byduser.username', title: __('用户'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'cztime', title: __('操作时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'level', title: __('身份级别')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'state', title: __('是否提供服务'), searchList: {"0":__('不提供'),"1":__('提供'),"2":__('未处理')}, formatter: Table.api.formatter.status},
                        {field: 'qy', title: __('审核状态'), searchList: {"0":__('未审核'),"1":__('通过'),"2":__('拒绝')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'twotg/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'twotg/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'twotg/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});